//
//  KKMyController.m
//  DoMi
//
//  Created by 康凯 on 16/10/26.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKMyController.h"
#import "KKMyCell.h"
#import "KKMyHttp.h"
#import "KKMyHeaderView.h"
#import "KKJiFenController.h"
#import "KKAccountController.h"
#import "KKMyMessageController.h"
#import "KKBuyVideoVC.h"
#import "KKMyUploadController.h"
#import "KKMyCollectController.h"
#import "KKMyQuestionVC.h"
#import "KKConsultVC.h"
#import "KKFeedBackController.h"
#import "KKDraftController.h"
#import "KKExitCell.h"

@interface KKMyController ()<UITableViewDelegate,UITableViewDataSource>

@property(weak,nonatomic) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray * datas;

@property (nonatomic,strong) KKMyHeaderView *header;

@property (nonatomic,strong) UIViewController *selectView;

@property (nonatomic,copy) NSString *expertFlag;
@property (nonatomic,strong) KKUserInfoM *user;



@end

@implementation KKMyController

-(NSMutableArray *)datas{
    if(!_datas){
        _datas=[NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithHexString:@"#EAEBEC"];
    //初始化nav
    [self setNavBlack];
    [self initView];
    [self loadData];
    
}

-(void)initView{
    self.expertFlag=@"0";
    KKMyHeaderView *header=[[[NSBundle mainBundle] loadNibNamed:@"KKMyHeaderView" owner:self options:nil] lastObject];
    self.header=header;

    UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight) style:UITableViewStyleGrouped];
    self.tableView=tableView;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.showsVerticalScrollIndicator=NO;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.sectionFooterHeight = 1;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 30, 0);
    [self.view addSubview:tableView];
    
}

-(void)loadData{
    NSArray * arr1 = @[@"我的帐户",@"我的积分",@"我的消息"];
    NSArray * arr2 = @[@"我购买的视频",@"我的上传",@"我的收藏"];
    NSArray * arr3 = @[@"我的悬赏/提问",@"我的咨询",@"草稿箱",@"意见反馈"];
    
    self.datas = [@[arr1,arr2,arr3] mutableCopy];
}


#pragma mark --tableview代理

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count+1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.datas.count==section){
        return 1;
    }else{
        NSArray *arr= self.datas[section];
        return arr.count;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==self.datas.count){
        KKExitCell * cell = [[[NSBundle mainBundle] loadNibNamed:@"KKExitCell" owner:nil options:nil] lastObject];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        KKMyCell * cell = [[[NSBundle mainBundle] loadNibNamed:@"KKMyCell" owner:nil options:nil] lastObject];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        NSString *title = self.datas[indexPath.section][indexPath.row];
        cell.title.font=[UIFont systemFontOfSize:16];
        cell.title.text=title;
        if(indexPath.section==0&&indexPath.row==2){
            if([self.user.wdxx isEqualToString:@"0"]){
                cell.msgCount.hidden=YES;
            }else{
                cell.msgCount.hidden=NO;
                //            [cell.msgCount setTitle:self.user.wdxx forState:UIControlStateNormal];
            }
        }else{
            cell.msgCount.hidden=YES;
        }
        return cell;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return self.header;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 268;
    }
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    long section=indexPath.section;
    long row=indexPath.row;
    if(section==0){
        if(row==0){
            KKAccountController *account=[[KKAccountController alloc] init];
            self.selectView=account;
        }else if(row==1){
            KKJiFenController *jiFen=[[KKJiFenController alloc] init];
            self.selectView=jiFen;
        }else{
            KKMyMessageController *msg=[[KKMyMessageController alloc] init];
            self.selectView=msg;
        }
    }else if(section==1){
        if(row==0){
            KKBuyVideoVC *buyVideo=[[KKBuyVideoVC alloc] init];
            self.selectView=buyVideo;
        }else if(row==1){
            KKMyUploadController *upload=[[KKMyUploadController alloc] init];
            self.selectView=upload;
        }else{
            KKMyCollectController *collect=[[KKMyCollectController alloc] init];
            self.selectView=collect;
        }
    }else if(section==2){
        if(row==0){
            KKMyQuestionVC *question=[[KKMyQuestionVC alloc] init];
            self.selectView=question;
        }else if(row==1){
            KKConsultVC *consult=[[KKConsultVC alloc] init];
            consult.expertFlag=self.expertFlag;
            self.selectView=consult;
        }else if(row==2){
            KKDraftController *draft=[[KKDraftController alloc] init];
            self.selectView=draft;
        }else{
            KKFeedBackController *feedBack=[[KKFeedBackController alloc] init];
            self.selectView=feedBack;
        }
    }
    [self.navigationController pushViewController:self.selectView animated:YES];
}

#pragma makr 接口--用户信息
-(void)getUserInfo{
    [KKMyHttp getUserInfoSuccess:^(id data) {
        self.user=data;
        self.expertFlag=self.user.expertflag;
        [self.header initData:data];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark view消失出现操作
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [self getUserInfo];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear: animated];
    self.navigationController.navigationBarHidden = NO;
}

@end
